﻿using System;
using System.Globalization;
using System.IO;

namespace XTrace
{
	[Obsolete]
	public abstract class TextXTraceListener : XTraceListener
	{
		protected void WriteColumns()
		{
			WriteLine(XTraceEvent.Columns(Format));
		}

		bool _writedColumns;
		protected void EnsureWriteColumns()
		{
			if(!_writedColumns)
			{
				_writedColumns = true;
				WriteColumns();
			}
		}

		protected abstract void WriteLine(string line);

		string _format = XTraceEvent.DefaultFormat;

		public virtual string Format
		{
			get
			{
				return _format;
			}
			set
			{
				_format = value;
				WriteColumns();
			}
		}

		static string Limit(string inp, int limit)
		{
			if (inp == null)
			{
				return null;
			}
			if (inp.Length > limit)
			{
				return inp.Substring(0, limit);
			}
			return inp;
		}

		public int ThreadNameLimit = 10;

		public override void Event(XTraceEvent traceEvent)
		{
			var message = traceEvent.Format(Format);
			WriteLine(message);
		}

	}
}